<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button onclick="debounceClick()">点击</button>
  </body>
  <script>
    function myDebounce(fn, delay) {
      let timer;
      return function () {
        //返回函数
        let context = this; //保存上下文
        let args = arguments; //保存参数
        if (timer) {
          clearTimeout(timer); //每次点击重制定时器
        }
        timer = setTimeout(() => {
          //新建定时器
          fn.apply(context, args); //执行函数
        }, delay);
      };
    }

    function click() {
      console.log("点击了");
    }

    const debounceClick = myDebounce(click, 3000);
  </script>
</html>
